package com.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.user.bean.Page;
import com.user.bean.User;
import com.user.dao.UserDao;
import com.user.service.UserService;

/**
 *  This is user service implements
 * @author LIU
 * @version 1.0 2017-09-21
 */
@Service
public class UserServiceImpl implements UserService {
	/**
	 * Create userDaoImpl object
	 */
	@Autowired
	UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * show all users
	 */
	@Override
	@Transactional(readOnly=true)
	public List<User> showAll() {
		// TODO Auto-generated method stub
		return userDao.showAll();
	}

	/**
	 * user login
	 */
	@Override
	@Transactional(readOnly=true)
	public boolean login(User user) {
		// TODO Auto-generated method stub
		if(userDao.login(user) > 0){
			return true;
		}
		return false;
	}

	/**
	 * Add user
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int addUser(User user) {
		// TODO Auto-generated method stub
		return userDao.addUser(user);
	}

	/**
	 * Change user information
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateUser(User user, String userId) {
		// TODO Auto-generated method stub
		if(userDao.updateUser(user, userId) > 0){
			return true;
		}
		return false;
	}

	/**
	 * Search user by Id
	 */
	@Override
	@Transactional(readOnly=true)
	public User searchUser(String userId) {
		// TODO Auto-generated method stub
		return userDao.searchUser(userId);
	}

	/**
	 * Delete user by Id
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int deleteUser(String userId) {
		// TODO Auto-generated method stub
		return userDao.deleteUser(userId);
	}

	/**
	 * Get all counts of users
	 */
	@Override
	@Transactional(readOnly=true)
	public int searchAllCount() {
		// TODO Auto-generated method stub
		return userDao.getAllCounts();
	}

	
	/**
	 * Get user by page
	 */
	@Override
	@Transactional(readOnly=true)
	public List<User> getUserByPage(Page p) {
		// TODO Auto-generated method stub
		return userDao.showAll(p);
	}

}
